package com.wk.warehouse.service.impl;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wk.warehouse.entity.Auth;
import com.wk.warehouse.entity.User;
import com.wk.warehouse.mapper.AuthMapper;
import com.wk.warehouse.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImp implements AuthService {
	@Autowired
	private AuthMapper authMapper;

	/**
	 * 查询所有权限层级列表
	 */
	@Override
	public List<Auth> firstAuthList() {
		return authMapper.firstAuthList();
	}

	/**
	 * 查询用户对应的权限
	 */
	@Override
	public List<Auth> findFirstAuth(int userId) {
		HashMap<String, Integer> hashMap = new HashMap<>();
		hashMap.put("userId", userId);
		// 一级权限
		List<Auth> firstList = authMapper.findFirstAuth(hashMap);
		StringBuffer sb = new StringBuffer();
		for (Auth firstAuth : firstList) {
			int authId = firstAuth.getAuthId();
			hashMap.put("authId", authId);
			// 根据一级权限的id查询二级权限
			List<Auth> secondList = authMapper.findChildAuth(hashMap);
			// 将二级权限放入一级权限集合属性中
			firstAuth.setChildAuth(secondList);
			// 循环遍历二级权限
			for (Auth secondAuth : secondList) {
				// 根据二级权限的id查询三级权限
				int secondAuthId = secondAuth.getAuthId();
				hashMap.put("authId", secondAuthId);
				// 查询子权限，通过二级找三级
				List<Auth> thirdList = authMapper.findChildAuth(hashMap);
				// 将三级权限放入二级权限集合属性中
				for (Auth thirdAuth : thirdList) {
					String authCode = thirdAuth.getAuthCode();
					sb.append(authCode+",");
				}
				secondAuth.setChildAuth(thirdList);
			}
		}
		return firstList;
	}

	/**
	 * 通过父id查询子权限
	 */
	@Override
	public List<Auth> findChildAuth(HashMap<String, Integer> hashMap) {
		return authMapper.findChildAuth(hashMap);
	}

	/**
	 * 根据父id查询所有子权限
	 */
	@Override
	public List<Auth> selectAllChild(int parentId) {
		return authMapper.selectAllChild(parentId);
	}

	/**
	 * 递归查询子权限
	 */
	@Override
	public JSONArray selectAllAuth(HttpServletRequest request, int parentId, JSONArray jsonArray) {
		List<Auth> firstList = authMapper.selectAllChild(parentId);
		// 遍历一级权限
		if(firstList.size() > 0){
			for (Auth firstauth : firstList) {
				// 查询一级权限的id
				int firsttId = firstauth.getAuthId();
				JSONObject firstJson = new JSONObject();
				if (firstauth.getAuthState().equals("0")) {
					firstJson.put("name", firstauth.getAuthName());
					firstJson.put("font", "{'color':'#A9A9A9'}");
				} else {
					firstJson.put("name", firstauth.getAuthName());
				}
				firstJson.put("id", firsttId);
				firstJson.put("pId", firstauth.getParentId());
				firstJson.put("open", false);
				jsonArray.add(firstJson);
				
				jsonArray = selectAllAuth(request,firsttId,jsonArray);
			}
		}
		return jsonArray;
	}

	/**
	 * 检查name是否唯一
	 */
	@Override
	public int findAuthByName(String authName) {
		return authMapper.findAuthByName(authName);
	}

	/**
	 * 检查url是否唯一
	 */
	@Override
	public int findAuthByUrl(String authUrl) {
		return authMapper.findAuthByUrl(authUrl);
	}

	/**
	 * 检查code是否唯一
	 */
	@Override
	public int findAuthByCode(String authCode) {
		return authMapper.findAuthByCode(authCode);
	}

	/**
	 * 添加权限
	 */
	@Override
	public int insertAuth(Auth auth) {
		return authMapper.insertAuth(auth);
	}

	/**
	 * 修改权限状态
	 */
	@Override
	public void updateState(int authId, int authState) {
		HashMap<String, Object> map = new HashMap<>();
		map.put("authId", authId);
		map.put("authState", authState);
		// 修改当前权限状态
		authMapper.updateState(map);
		// 如果有子权限，修改子权限的状态
		List<Auth> childAuthList = authMapper.selectAllChild(authId);
		if(childAuthList!=null && childAuthList.size()>0){
			for(Auth child: childAuthList){
				int count = authMapper.selectAuthByPid(child.getAuthId());
				if(count>0){
					updateState(child.getAuthId(), authState);
				}else{
					map.put("authId", child.getAuthId());
					authMapper.updateState(map);
				}
			}
		}
	}

	/**
	 * 删除权限
	 */
	@Override
	public void deleteAuth(int authId) {
		// 删除当前权限
		authMapper.deleteAuth(authId);
		// 如果有子权限，删除子权限
		List<Auth> childAuthList = authMapper.selectAllChild(authId);
		if(childAuthList!=null && childAuthList.size()>0){
			for(Auth child: childAuthList){
				int count = authMapper.selectAuthByPid(child.getAuthId());
				if(count>0){
					deleteAuth(child.getAuthId());
				}else{
					authMapper.deleteAuth(child.getAuthId());
				}
			}
		}
	}

	/**
	 * 修改权限
	 */
	@Override
	public int updateAuth(Auth auth) {
		return authMapper.updateAuth(auth);
	}

	/**
	 * 通过权限id查找权限
	 */
	@Override
	public Auth findAuthById(int authId) {
		return authMapper.findAuthById(authId);
	}
	/**
	 * 通过父Id查找权限
	 */
	@Override
	public int selectAuthByPId(int parentId) {
		return authMapper.selectAuthByPid(parentId);
	}

}
